﻿using Coldairarrow.Entity.Goods;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Goods
{
    public class BTB_GoodsTypeBusiness : BaseBusiness<BTB_GoodsType>, IBTB_GoodsTypeBusiness, ITransientDependency
    {
        readonly IOperator _operator;

        public BTB_GoodsTypeBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BTB_GoodsType>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BTB_GoodsType>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BTB_GoodsType, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<BTB_GoodsType> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }
        public async Task AddDataAsync(BTB_GoodsType data)
        {
            var id = GuidHelper.GenerateKey();
            string Parent = string.Empty;
            var aa = GetIQueryable().Where(a => a.id == data.Parent).FirstOrDefault();
            if (aa != null)
            {
                Parent = aa.RootParent;
                if (Parent != "0")
                {
                    BTB_GoodsType data1 = new BTB_GoodsType()
                    {
                        id = id,
                        CreateTime = data.CreateTime,
                        CreatorId = _operator.UserId,
                        Editor = data.Editor,
                        EditTime = data.EditTime,
                        Deleted = false,
                        TypeName = data.TypeName,
                        Icon = data.Icon,
                        IconImage = data.IconImage,
                        Parent = data.Parent,
                        RootParent = Parent,
                        Sort = data.Sort,
                        key = id,
                        value = id
                    };

                    await InsertAsync(data1);
                }
                else
                {
                    BTB_GoodsType data2 = new BTB_GoodsType()
                    {
                        id = id,
                        CreateTime = data.CreateTime,
                        CreatorId = _operator.UserId,
                        Editor = data.Editor,
                        EditTime = data.EditTime,
                        Deleted = false,
                        TypeName = data.TypeName,
                        Parent = data.Parent,
                        IconImage = data.IconImage,
                        Icon = data.Icon,
                        RootParent = data.RootParent,
                        Sort = data.Sort,
                        key = id,
                        value = id
                    };
                    await InsertAsync(data2);
                }

            }
            else
            {
                Parent = data.Parent;
                BTB_GoodsType data2 = new BTB_GoodsType()
                {
                    id = id,
                    CreateTime = data.CreateTime,
                    CreatorId = _operator.UserId,
                    Editor = data.Editor,
                    EditTime = data.EditTime,
                    Deleted = false,
                    IconImage = data.IconImage,
                    TypeName = data.TypeName,
                    Parent = data.Parent,
                    Icon = data.Icon,
                    RootParent = Parent,
                    Sort = data.Sort,
                    key = id,
                    value = id
                };
                await InsertAsync(data2);
            }


        }

        public async Task UpdateDataAsync(BTB_GoodsType data)
        {
            BTB_GoodsType data1 = new BTB_GoodsType()
            {
                id = data.id,
                CreateTime = data.CreateTime,
                CreatorId = _operator.UserId,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Deleted = false,
                TypeName = data.TypeName,
                IconImage = data.IconImage,
                Parent = data.Parent,
                Icon = data.Icon,
                RootParent = data.RootParent,
                Sort = data.Sort,
            };
            await UpdateAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
            foreach (var item in ids)
            {
                var aa = Db.GetIQueryable<BTB_GoodsType>(true).Where(a => a.RootParent == item || a.Parent == item).ToList();
                await Db.DeleteAsync<BTB_GoodsType>(aa);
            }
        }
        public List<BTB_GoodsType> All()
        {
            var aa = GetIQueryable().ToList();

            var level1 = aa.Where(a => a.Parent == "0").OrderByDescending(q => q.Sort)
                    .Select(a =>
                    {
                        a.children = aa.Where(b => b.Parent == a.id).OrderByDescending(b => b.Sort).ToList()
                            .Select(b =>
                            {
                                b.children = aa.Where(c => c.Parent == b.id).OrderByDescending(c => c.Sort).ToList();
                                return b;
                            }).ToList();
                        return a;
                    }).ToList();

            foreach (var item in level1)
            {
                item.key = item.id;
                item.value = item.id;

                if (item.children.Count == 0)
                {

                    item.children = new List<BTB_GoodsType>();
                }
                else
                {
                    foreach (var item2 in item.children)
                    {
                        item2.key = item2.id;
                        item2.value = item2.id;

                        if (item2.children?.Count == 0)
                        {

                            item2.children = new List<BTB_GoodsType>();
                        }
                        else
                        {
                            foreach (var item3 in item2.children)
                            {
                                item3.key = item3.id;
                                item3.value = item3.id;

                                if (item3.children?.Count == 0)
                                {

                                    item3.children = new List<BTB_GoodsType>();
                                }
                            }
                        }
                    }
                }

            }

            return level1;
        }
        public List<BTB_GoodsType> AllOneBackstage()
        {
            var aa = GetIQueryable().ToList();

            var level1 = aa.Where(a => a.Parent == "0").OrderByDescending(q => q.Sort).ToList();
            return level1;
        }
        public List<BTB_GoodsType> AllBackstage()
        {
            var aa = GetIQueryable().ToList();

            var level1 = aa.Where(a => a.Parent == "0").OrderByDescending(q => q.Sort)
                    .Select(a =>
                    {
                        a.children = aa.Where(b => b.Parent == a.id).ToList();
                        return a;
                    }).ToList();
            return level1;
        }
        #endregion

        #region 私有成员

        #endregion
    }
}